package com.genesis.web.controller;



import com.genesis.mall.common.Result;
import com.genesis.mall.dto.OrderDetailDto;
import com.genesis.mall.dto.OrderParam;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.OmsOrder;

import com.genesis.mall.service.IOmsOrderItemService;
import com.genesis.mall.service.IOmsOrderService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
//TODO 前后台冲突
@RestController
@RequestMapping("/order")
public class OmsOrderController {
    @Autowired
    private IOmsOrderService omsOrderService;

    @Autowired
    private IOmsOrderItemService orderItemService;

    /**
     * 取消订单
     *
     * @param orderId
     * @return
     */
    @PostMapping("/cancelUserOrder")
    public Result<String> cancelUserOrder(Long orderId) {
        omsOrderService.cancelUserOrder(orderId);
        return Result.success();
    }

    /**
     * 确认收货
     *
     * @param orderId
     * @return
     */
    @PostMapping("/confirmReceiveOrder")
    public Result<String> confirmReceiveOrder(Long orderId) {
        //根据id修改收货状态
        OmsOrder omsOrder = new OmsOrder();
        omsOrder.setId(orderId);
        omsOrder.setConfirmStatus(1);
        omsOrder.setReceiveTime(LocalDateTime.now());
        omsOrderService.updateById(omsOrder);
        return Result.success();
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @PostMapping("/deleteOrder")
    public Result<String> deleteOrder(Long orderId) {
        //根据id修改删除状态
        OmsOrder omsOrder = new OmsOrder();
        omsOrder.setId(orderId);
        omsOrder.setDeleteStatus(1);
        omsOrderService.updateById(omsOrder);
        return Result.success();
    }

    /**
     * 根据ID获取订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public Result<OrderDetailDto> detail(Long orderId) {
        OrderDetailDto orderDetailDto = omsOrderService.detail(orderId);
        return Result.success(orderDetailDto);
    }

    /**
     * 根据购物车信息生成确认单
     * @param param
     * @param request
     * @return
     */
    @PostMapping("/generateConfirmOrder")
    public Result<Map> generateConfirmOrder(@RequestBody Map<String, List<Long>> param, HttpServletRequest request) {

        Map<String, Object> map = omsOrderService.generateConfirmOrder(param.get("cartIds"), request);
        return Result.success(map);
    }

    /**
     * 按状态分页获取用户订单列表
     */
    @GetMapping("/list")
    public Result<PageDto<OrderDetailDto>> list(PageDto<OrderDetailDto> pageDto,HttpServletRequest request){
        return Result.success(omsOrderService.getList(pageDto,request));
    }

    /**
     * 用户支付成功的回调
     */
    @PostMapping("/paySuccess")
    public Result<String> paySuccess(OmsOrder omsOrder){
        //根据orderId修改订单表数据
        omsOrder.setPaymentTime(LocalDateTime.now());
        omsOrderService.updateById(omsOrder);
        return Result.success();
    }

    /**
     * 根据购物车信息生成订单
     */
    @PostMapping("/generateOrder")
    public Result<Map<String,Object>> generateOrder(@RequestBody OrderParam orderParam, HttpServletRequest request){
        Map<String, Object> order = omsOrderService.generateOrder(orderParam, request);
        return Result.success(order);
    }

}
